package it.globalgamejam.scar.game.world 
{
	import it.globalgamejam.scar.Assets;
	import it.globalgamejam.scar.game.GameWorld;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Mouse extends Entity
	{
		private var spritemap:Spritemap = new Spritemap(Assets.MOUSE, 60, 60);
		
		private var dir:int = 0; // 0=top, 1=right, 2=bottom, 3=left
		private var deltaTime:Number = 0;
		public var blocked:Boolean = false;
		
		public function Mouse(dir:int, x:Number=0, y:Number=0) 
		{
			super(x + 15, y + 15);
			
			spritemap.add("appear", [5, 4, 3, 2, 1, 2, 3, 4, 5], 20, false);
			spritemap.scale = 2;
			
			graphic = spritemap;
			
			if (dir == 0 || dir == 2)
				setHitbox(60, 120, -30, 0);
			else
				setHitbox(120, 60, 0, -30);
			
			type = "mouse";
		}
		
		public function blockHole():void {
			graphic = new Image(Assets.BLOCKED_HOLE);
			blocked = true;
		}
		
		public static function createMouse(dir:int):Mouse {
			return new Mouse(dir);
		}
		
		public override function update():void {
			super.update();
			
			if (!blocked) {
				deltaTime += FP.elapsed;
				if (deltaTime >= 1.5) {
					deltaTime -= 1.5;
					spritemap.play("appear", true);
				} else if (deltaTime <= 0.5) {
					var player:Player = FP.world.getInstance("player") as Player;
					if (player) {
						if (collideWith(player, x, y)) {
							GameWorld(FP.world).levelLost();
						}
					}
					
					var ball:Ball = FP.world.getInstance("ball") as Ball;
					if (ball) {
						if (collideWith(ball, x, y)) {
							GameWorld(FP.world).levelLost();
						}
					}
				}
			}
		}
	}

}